# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

_realname=openssl
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
_ver=1.1.1i
# use a pacman compatible version scheme
pkgver=${_ver/[a-z]/.${_ver//[0-9.]/}}
pkgrel=1
arch=('any')
pkgdesc="The Open Source toolkit for Secure Sockets Layer and Transport Layer Security (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-ca-certificates" "${MINGW_PACKAGE_PREFIX}-gcc-libs" "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
options=('strip' '!buildflags' 'staticlibs')
license=('custom:BSD')
url="https://www.openssl.org"
noextract=(${_realname}-${_ver}.tar.gz)
source=(https://www.openssl.org/source/${_realname}-${_ver}.tar.gz{,.asc}
        'openssl-1.1.1-relocation.patch')
sha256sums=('e8be6a35fe41d10603c3cc635e93289ed00bf34b79671a3a4de64fcee00d5242'
            'SKIP'
            '798d1b4dec28429d6cf2081c5b5b23b4344590a9b812e8089842389dbd175b77')

validpgpkeys=('8657ABB260F056B1E5190839D9C4D26D0E604491'
              '7953AC1FBC3DC8B3B292393ED5E9E43F7DF9EE8C')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  # Clean up old sources so re-patching doesn't fail.
  [[ -d ${srcdir}/${_realname}-${_ver} ]] && rm -rf ${srcdir}/${_realname}-${_ver}
  tar -xzvf ${_realname}-${_ver}.tar.gz -C ${srcdir} || true
  cd ${srcdir}/${_realname}-${_ver}

  apply_patch_with_msg \
     openssl-1.1.1-relocation.patch
}

build() {
  rm -rf ${srcdir}/build-${CARCH}

  # No support for out-of-source builds
  mkdir -p ${srcdir}/build-${CARCH}
#  cp -a ${srcdir}/${_realname}-${_ver}/* ${srcdir}/build-${CARCH}

  # Use mingw cflags instead of hardcoded ones
  sed -i -e '/^"mingw"/ s/-fomit-frame-pointer -O3 -Wall/-O2 -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions --param=ssp-buffer-size=4/' \
     ${srcdir}/${_realname}-${_ver}/Configure
#     ${srcdir}/build-${CARCH}/Configure

  _mingw=mingw
  if $(echo ${CARCH} | grep -q x86_64); then
    _mingw=mingw64
  fi
  cd "${srcdir}/build-${CARCH}"
  export MSYS2_ARG_CONV_EXCL="--prefix="
  ${srcdir}/${_realname}-${_ver}/Configure \
    --prefix=${MINGW_PREFIX} \
    --openssldir=ssl \
    ${_mingw} \
    shared \
    zlib-dynamic \
    enable-camellia \
    enable-capieng \
    enable-idea \
    enable-mdc2 \
    enable-rc5  \
    enable-rfc3779 \
    -D__MINGW_USE_VC2005_COMPAT \
    -DOPENSSLBIN=\"\\\"${MINGW_PREFIX}/bin\\\"\"

  make ZLIB_INCLUDE=-I"${MINGW_PREFIX}"/include depend all
}

check() {
  cd "${srcdir}/build-${CARCH}"
  make VERBOSE=1 test
}

package() {
  cd "${srcdir}/build-${CARCH}"
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/bin
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/include/openssl
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/lib/engines-1_1
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}

  make -j1 DESTDIR="${pkgdir}" MANDIR="${MINGW_PREFIX}/share/man" MANSUFFIX=ssl install
  install -D -m644 "${srcdir}/${_realname}-${_ver}/LICENSE" \
         "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE

  chmod -R 777 "${pkgdir}${MINGW_PREFIX}"/bin
  chmod -R 777 "${pkgdir}${MINGW_PREFIX}"/lib
  chmod -R 777 "${pkgdir}${MINGW_PREFIX}"/lib/engines-1_1

  install -m644 "${srcdir}/${_realname}-${_ver}/ms/applink.c" "${pkgdir}${MINGW_PREFIX}"/include/openssl/
  rm -rf "${pkgdir}${MINGW_PREFIX}"/bin/c_rehash
  rm -rf "${pkgdir}${MINGW_PREFIX}"/{ssl/misc,ssl/man}
}
